package raft

import (
	"fmt"
	"strings"
)

func (l *RaftLog) pos(idx uint64) (uint64, error) {
	elen := len(l.entries)
	if elen == 0 {
		return 0, ErrCompacted
	}
	e := l.entries[0]
	if idx < e.Index {
		return 0, ErrCompacted
	}
	off := idx - e.Index
	//比较差值和原来的容量
	if off >= uint64(elen) {
		return 0, ErrUnavailable
	}
	return off, nil
}

func (r *Raft) prs2string() string {
	var builder strings.Builder
	builder.WriteString(fmt.Sprintf("Prs(%d){", len(r.Prs)))
	for id, pr := range r.Prs {
		builder.WriteString(fmt.Sprintf("%d:{%d,%d},", id, pr.Match, pr.Next))
	}
	builder.WriteByte('}')
	return builder.String()
}
